//
//  TodaySleepTableViewCell.swift
//  MoyoungCommon
//
//  Created by queue on 2022/7/19.
//

import Foundation
import MYModule
import RxSwift
import RxCocoa
import UIKit


class HomeSleepCell: HomeBaseCell {
    
    
    override func makeUI() {
        super.makeUI()
        
        let title = "sleep_title".language
        let hourUntil = "unit_upper_case_hour".language
        let minuteUntil = "unit_upper_case_minute".language
        let iconName = "ic_home_today_sleep"
        
        iconImagView.image = UIImage(named: iconName)
        titleLabel.text = title
        minutesUnitLabel.text = minuteUntil
        hourUnitLabel.text = hourUntil
        
        
        bgView.addSubview(hourNumberLabel)
        bgView.addSubview(hourUnitLabel)
        bgView.addSubview(minutesNumberLabel)
        bgView.addSubview(minutesUnitLabel)
        bgView.addSubview(targetHourLabel)
        bgView.addSubview(chartView)
        bgView.addSubview(emptyView)
        bgView.addSubview(fallAsleepLabel)
        bgView.addSubview(wakeUpLabel)
        
        
        minutesUnitLabel.snp.makeConstraints { make in
            make.right.equalTo(bgView.snp.right).offset(-15)
            make.bottom.equalTo(minutesNumberLabel.snp.bottom).offset(-2)
        }
        
        minutesNumberLabel.snp.makeConstraints { make in
            make.bottom.equalTo(titleLabel.snp.bottom)
            make.right.equalTo(minutesUnitLabel.snp.left).offset(-4)
        }
        
        hourUnitLabel.snp.makeConstraints { make in
            make.right.equalTo(minutesNumberLabel.snp.left).offset(-4)
            make.bottom.equalTo(minutesUnitLabel.snp.bottom)
        }
        
        hourNumberLabel.snp.makeConstraints { make in
            make.bottom.equalTo(minutesNumberLabel.snp.bottom)
            make.right.equalTo(hourUnitLabel.snp.left).offset(-4)
        }
        
        targetHourLabel.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(dateLabel.snp.centerY)
        }
       
        chartView.snp.makeConstraints { make in
            make.height.equalTo(110)
            make.leading.equalToSuperview().offset(15)
            make.trailing.equalToSuperview().offset(-15)
            make.top.equalTo(dateLabel.snp.bottom).offset(26)
        }
        
        emptyView.snp.makeConstraints { make in
            make.center.equalTo(chartView.snp.center)
        }
        
        fallAsleepLabel.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            make.top.equalTo(chartView.snp.bottom).offset(11)
            make.bottom.equalTo(bgView.snp.bottom).offset(-20)
        }
        
        wakeUpLabel.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalTo(fallAsleepLabel)
        }
       
        self.titleLabel.textColor =  UIColor.assist_222222
        
        self.minutesNumberLabel.textColor = UIColor.Sleep.theme
        self.minutesUnitLabel.textColor =  UIColor.assist_222222
        
        self.bgView.widgetId = "cv_sleep_bg"
        self.titleLabel.widgetId = "tv_sleep_title"
        self.dateLabel.widgetId = "tv_sleep_time"
        self.targetHourLabel.widgetId = "tv_sleep_hour"
        self.hourUnitLabel.widgetId = "tv_sleep_hour_unit"
        self.minutesNumberLabel.widgetId = "tv_sleep_minutes"
        self.minutesUnitLabel.widgetId = "tv_sleep_minutes_unit"
        
        self.chartView.widgetId = "chart_sleep"
        self.fallAsleepLabel.widgetId = "tv_sleep_fall_asleep_time"
        self.wakeUpLabel.widgetId = "tv_sleep_wake_up_time"
        self.iconImagView.widgetId = "bg_sleep_icon"
        
    }
    
    override func bind(to viewModel: ViewModel?) {
        super.bind(to: viewModel)
        
        guard let viewModel = viewModel as? HomeSleepViewModel else { return }
        
        let disposeBag = DisposeBag()
        
        viewModel.UIRelay.subscribe(onNext: {[weak self] model in
            
            self?.dateLabel.text = model.date
            
            self?.hourNumberLabel.text = model.hourTitle
            
            self?.minutesNumberLabel.text = model.minuteTitle
            self?.targetHourLabel.text = "sleep_target_hours".language(with: ["\(model.targetHour)"])
            if model.hasData {
                self?.fallAsleepLabel.text = "sleep_fail_asleep".language(with: [model.fallAsleep])
                self?.wakeUpLabel.text = "sleep_wake_up".language(with: [model.wakeUp])
                self?.emptyView.isHidden = true
                self?.chartView.isHidden = false
            } else {
                self?.fallAsleepLabel.text = nil
                self?.wakeUpLabel.text = nil
                self?.emptyView.isHidden = false
                self?.chartView.isHidden = true
            }
           
            self?.fallAsleepLabel.isHidden = !model.hasData
            self?.wakeUpLabel.isHidden = !model.hasData
            
            
            self?.updateLayout(model: model)
            
            
            self?.chartView.data = model.lineData
            
        }).disposed(by: disposeBag)
        
    }
    
    func updateLayout(model: HomeSleepModel) {
        
        fallAsleepLabel.snp.remakeConstraints { make in
            make.leading.equalToSuperview().offset(15)
            
            if model.hasData {
                
                make.top.equalTo(chartView.snp.bottom).offset(11)
                make.bottom.equalTo(bgView.snp.bottom).offset(-20)
                
            } else {
                
                make.top.equalTo(chartView.snp.bottom)
                make.bottom.equalTo(bgView.snp.bottom)
            }
        }
        
    }
    
    //MARK: - lazy load -
    
    public lazy var emptyView: EmptyView = {
        
        let emptyView = EmptyView()
        emptyView.label.text = "sleep_no_information".language
        
        return emptyView
        
    }()
    
    public lazy var minutesNumberLabel: Label = {
        let minutesNumberLabel = Label()
        minutesNumberLabel.textAlignment = .right
        minutesNumberLabel.font = Fonts.today_title_lv_2(.medium)
        return minutesNumberLabel
    }()
    
    public lazy var minutesUnitLabel: Label = {
        let untilLbl = Label()
        untilLbl.textAlignment = .right
        untilLbl.font = Fonts.today_content_lv_3()
        return untilLbl
    }()
    
    private lazy var hourNumberLabel: Label = {
        let hourNumberLabel = Label()
        hourNumberLabel.textColor = UIColor.Sleep.theme
        hourNumberLabel.font = Fonts.today_title_lv_2(.medium)
        return hourNumberLabel
    }()
    
    public lazy var hourUnitLabel: Label = {
        let hourUnitLabel = Label()
        hourUnitLabel.textAlignment = .right
        hourUnitLabel.font = Fonts.today_content_lv_3()
        return hourUnitLabel
    }()
    
   
    private lazy var targetHourLabel: Label = {
        let hourLbl = Label()
        hourLbl.textColor = UIColor.assist_D0D0D0
        hourLbl.font = Fonts.systemFont(ofSize: 10)
        hourLbl.textAlignment = .right
        return hourLbl
    }()
    
    
    private lazy var chartView: SegmentLineChartView = {
        let view = SegmentLineChartView()
        view.colors = UIColor.Sleep.sleep_gradual_change
        return view
    }()
    
    private lazy var fallAsleepLabel: Label = {
        let lbl = Label()
        lbl.textColor = UIColor.assist_D0D0D0
        lbl.font = Fonts.systemFont(ofSize: 10)
        lbl.textAlignment = .left
        return lbl
    }()
    
    private lazy var wakeUpLabel: Label = {
        let lbl = Label()
        lbl.textColor = UIColor.assist_D0D0D0
        lbl.font = Fonts.systemFont(ofSize: 10)
        lbl.textAlignment = .right
        return lbl
    }()
    

    
  
    
}
